package com.cac.demo.newCode.cemopsim.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.cac.demo.newCode.cemop.dto.CemopXmlDTO;
import com.cac.demo.newCode.cemop.enums.CemopXmlEnum;
import com.cac.demo.newCode.cemop.utils.CemopXmlUtil;
import com.cac.demo.newCode.cemopsim.mapper.CemopSimMapper;
import com.cac.demo.newCode.cemopsim.service.CemopSimService;
import com.cac.demo.newCode.cemop.vo.CemopCancelDeliveryVO;
import com.cac.demo.util.adapter.ResponseMsg;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import static org.jsoup.nodes.Document.OutputSettings.Syntax.xml;

/**
 * @author zty
 * @description
 * @date 2024/11/1
 */
@Slf4j
@Service
public class CemopSimServiceImpl implements CemopSimService {
    @Resource
    private CemopSimMapper cemopSimMapper;

    @Resource
    private CemopXmlUtil cemopXmlUtil;

    /**
     * 模拟CEMOP系统下发二次配套物料至101库存
     *
     * @return
     */
    @Override
    public ResponseMsg<Object> resSecondMatch() {
        try {
            // 查询二次配套信息
            List<HashMap> hashMaps = cemopSimMapper.selectCemopmomMaterialRecl();
            CemopXmlDTO cemopXmlDTO = new CemopXmlDTO();
            cemopXmlDTO.setXmlType(CemopXmlEnum.CANCEL_DELIVERY.getCode());
            List<CemopXmlDTO> cemopXmlDTOList = new ArrayList<>();
            for (HashMap<String, String> map : hashMaps) {
                // 创建 SimpleDateFormat 实例
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // 格式化日期
                String formattedDate = formatter.format(map.get("RECEIVE_DATE"));
                //取数据
                String MDSITEMID = map.get("MDS_ITEM_ID");
                String MOMDELIVERYPLANID = map.get("WMS_DELIVERY_PLAN_ID");
                String RECEUSER = map.get("RECEIVE_EMP_CODE");
                String RECEQTY = String.valueOf(map.get("RECEIVE_QTY"));
                String randomUUID = UUID.randomUUID().toString().replace("-","");
                //生成xml数据
                Map<String, String> hashMap = new HashMap();
                hashMap.put("MDS_ITEM_ID", MDSITEMID);
                hashMap.put("MOM_DELIVERY_PLAN_ID", MOMDELIVERYPLANID);
                hashMap.put("MOM_ASS_SECMATCH_DETAIL_ID", randomUUID);//二次配套主键
                hashMap.put("RECE_USER", RECEUSER);
                hashMap.put("RECE_DATE", formattedDate);
                hashMap.put("RECE_QTY", RECEQTY);
                CemopXmlDTO tableDTO = cemopXmlUtil.createTableDTO(map.get("TABLENAME"), hashMap);
                cemopXmlDTOList.add(tableDTO);
            }
            cemopXmlDTO.setCemopXmlDTOList(cemopXmlDTOList);
            JSONObject xml = cemopXmlUtil.createXml(cemopXmlDTO);
            if (xml == null) {
                log.error("生成XML失败！");
                return new ResponseMsg<>().setResponseBody(null).setMessage("生成XML失败").setRetCode("500");
            }
            return new ResponseMsg<>().setResponseBody(xml).setMessage("模拟CEMOP系统下发二次配套物料至101库存").setRetCode("200");
        } catch (Exception e) {
            log.error("Error in sendMatchBillInfo: ", e);
            return new ResponseMsg<>().setResponseBody(null).setMessage("系统错误").setRetCode("500");
        }
    }

    /**
     * 模拟月计划下发Cemop
     *
     * @return
     */
    @Override
    public ResponseMsg<Object> aoMonthPlan() {
        //查询月计划三个sql
        List<HashMap> hashMaps = cemopSimMapper.selectCemopmomAssAotask();
        List<HashMap> hashMaps1 = cemopSimMapper.selectCemopmomAssWorkpackage();
        List<HashMap> hashMaps2 = cemopSimMapper.selectCemopmomAssSetout();
        CemopXmlDTO cemopXmlDTO = new CemopXmlDTO();
        List<CemopXmlDTO> cemopXmlDTOList = new ArrayList<>();
        cemopXmlDTO.setXmlType(CemopXmlEnum.CANCEL_DELIVERY.getCode());
        // 创建一个新的Map来存储合并后的数据
        Map<String, Object> combinedMap = new HashMap<>();
        Map<String, Object> combinedMap2 = new HashMap<>();
        Map<String, Object> combinedMap3 = new HashMap<>();

        // 遍历第一个List并存入新的Map
        for (HashMap<String, Object> map : hashMaps) {
            //生成xml数据
            combinedMap.putAll(map);
            CemopXmlDTO tableDTO = cemopXmlUtil.createTableDTOs(map.get("TABLENAME"), combinedMap);
            cemopXmlDTOList.add(tableDTO);
        }

        // 遍历第二个List并存入新的Map
        for (HashMap<String, Object> map : hashMaps1) {
            combinedMap2.putAll(map);
            CemopXmlDTO tableDTO = cemopXmlUtil.createTableDTOs(map.get("TABLENAME"), combinedMap2);
            cemopXmlDTOList.add(tableDTO);
        }

        // 遍历第三个List并存入新的Map
        for (HashMap<String, Object> map : hashMaps2) {
            combinedMap3.putAll(map);
            CemopXmlDTO tableDTO = cemopXmlUtil.createTableDTOs(map.get("TABLENAME"), combinedMap3);
            cemopXmlDTOList.add(tableDTO);
        }
        cemopXmlDTO.setCemopXmlDTOList(cemopXmlDTOList);
        JSONObject xml = cemopXmlUtil.createXml(cemopXmlDTO);
        return new ResponseMsg<>().setResponseBody(xml).setMessage("模拟CEMOP系统下发二次配套物料至101库存").setRetCode("200");
    }

}
